home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter20 / isohex20_1 / isohex20_1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-19  |  7.1 KB  |  296 lines

  1. /*****************************************************************************
  2. IsoHex20_1.cpp
  3. Ernest S. Pazera
  4. 19SEP2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires GDICanvas.h and GDICanvas.cpp
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15. #include "GDICanvas.h"
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX20"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 20-1"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31. void ShowMap(HDC hdc);//show the map
  32.  
  33. //////////////////////////////////////////////////////////////////////////////
  34. //GLOBALS
  35. //////////////////////////////////////////////////////////////////////////////
  36. HINSTANCE hInstMain=NULL;//main application handle
  37. HWND hWndMain=NULL;//handle to our main window
  38.  
  39. //gdi canvases
  40. CGDICanvas gdicSquare;//square tile
  41. CGDICanvas gdicIso;//isometric tile
  42. CGDICanvas gdicIsoMask;//isometric mask
  43.  
  44. //which map to show
  45. bool bShowIso=true;//true if iso, false if square
  46.  
  47. //////////////////////////////////////////////////////////////////////////////
  48. //WINDOWPROC
  49. //////////////////////////////////////////////////////////////////////////////
  50. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  51. {
  52.     //which message did we get?
  53.     switch(uMsg)
  54.     {
  55.     case WM_KEYDOWN:
  56.         {
  57.             //if '1', show iso
  58.             if(wParam=='1')
  59.             {
  60.                 bShowIso=true;
  61.                 InvalidateRect(hWndMain,NULL,TRUE);
  62.             }
  63.             //if '2', show square
  64.             if(wParam=='2')
  65.             {
  66.                 bShowIso=false;
  67.                 InvalidateRect(hWndMain,NULL,TRUE);
  68.             }
  69.             return(0);//handled, so return 0
  70.         }break;
  71.     case WM_DESTROY://the window is being destroyed
  72.         {
  73.  
  74.             //tell the application we are quitting
  75.             PostQuitMessage(0);
  76.  
  77.             //handled message, so return 0
  78.             return(0);
  79.  
  80.         }break;
  81.     case WM_PAINT://the window needs repainting
  82.         {
  83.             //a variable needed for painting information
  84.             PAINTSTRUCT ps;
  85.             
  86.             //start painting
  87.             HDC hdc=BeginPaint(hwnd,&ps);
  88.  
  89.             /////////////////////////////
  90.             //painting code would go here
  91.             /////////////////////////////
  92.             ShowMap(hdc);
  93.  
  94.             //end painting
  95.             EndPaint(hwnd,&ps);
  96.  
  97.             //handled message, so return 0
  98.             return(0);
  99.         }break;
  100.     }
  101.  
  102.     //pass along any other message to default message handler
  103.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  104. }
  105.  
  106.  
  107. //////////////////////////////////////////////////////////////////////////////
  108. //WINMAIN
  109. //////////////////////////////////////////////////////////////////////////////
  110. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  111. {
  112.     //assign instance to global variable
  113.     hInstMain=hInstance;
  114.  
  115.     //create window class
  116.     WNDCLASSEX wcx;
  117.  
  118.     //set the size of the structure
  119.     wcx.cbSize=sizeof(WNDCLASSEX);
  120.  
  121.     //class style
  122.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  123.  
  124.     //window procedure
  125.     wcx.lpfnWndProc=TheWindowProc;
  126.  
  127.     //class extra
  128.     wcx.cbClsExtra=0;
  129.  
  130.     //window extra
  131.     wcx.cbWndExtra=0;
  132.  
  133.     //application handle
  134.     wcx.hInstance=hInstMain;
  135.  
  136.     //icon
  137.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  138.  
  139.     //cursor
  140.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  141.  
  142.     //background color
  143.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  144.  
  145.     //menu
  146.     wcx.lpszMenuName=NULL;
  147.  
  148.     //class name
  149.     wcx.lpszClassName=WINDOWCLASS;
  150.  
  151.     //small icon
  152.     wcx.hIconSm=NULL;
  153.  
  154.     //register the window class, return 0 if not successful
  155.     if(!RegisterClassEx(&wcx)) return(0);
  156.  
  157.     //create main window
  158.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  159.  
  160.     //error check
  161.     if(!hWndMain) return(0);
  162.  
  163.     //if program initialization failed, then return with 0
  164.     if(!Prog_Init()) return(0);
  165.  
  166.     //message structure
  167.     MSG msg;
  168.  
  169.     //message pump
  170.     for(;;)    
  171.     {
  172.         //look for a message
  173.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  174.         {
  175.             //there is a message
  176.  
  177.             //check that we arent quitting
  178.             if(msg.message==WM_QUIT) break;
  179.             
  180.             //translate message
  181.             TranslateMessage(&msg);
  182.  
  183.             //dispatch message
  184.             DispatchMessage(&msg);
  185.         }
  186.  
  187.         //run main game loop
  188.         Prog_Loop();
  189.     }
  190.     
  191.     //clean up program data
  192.     Prog_Done();
  193.  
  194.     //return the wparam from the WM_QUIT message
  195.     return(msg.wParam);
  196. }
  197.  
  198. //////////////////////////////////////////////////////////////////////////////
  199. //INITIALIZATION
  200. //////////////////////////////////////////////////////////////////////////////
  201. bool Prog_Init()
  202. {
  203.     //grab hdc from main window
  204.     HDC hdc=GetDC(hWndMain);
  205.  
  206.     //load the square tile
  207.     gdicSquare.Load(hdc,"IsoHex20_1.bmp");
  208.  
  209.     //create the iso tile
  210.     gdicIso.CreateBlank(hdc,64,32);
  211.     gdicIsoMask.CreateBlank(hdc,64,32);
  212.  
  213.     //scan convert the square image into the iso image
  214.     int tx,ty;//texture coordinates
  215.     int x,y;//pixel coordinates
  216.     int tempx;//for plotting a row of pixels
  217.     COLORREF crColor;
  218.  
  219.     //clear out iso image
  220.     for(x=0;x<64;x++)
  221.     {
  222.         for(y=0;y<32;y++)
  223.         {
  224.             SetPixelV(gdicIso,x,y,RGB(0,0,0));//iso is black
  225.             SetPixelV(gdicIsoMask,x,y,RGB(255,255,255));//mask is black
  226.         }
  227.     }
  228.  
  229.  
  230.     //loop through x texture coords
  231.     for(tx=0;tx<16;tx++)
  232.     {
  233.         //loop through y texture coords
  234.         for(ty=0;ty<16;ty++)
  235.         {
  236.             //grab the color from the texture
  237.             crColor=GetPixel(gdicSquare,tx,ty);
  238.             //calculate x and y
  239.             x=30+tx*2-ty*2;
  240.             y=tx+ty;
  241.             //loop through four pixel positions
  242.             for(tempx=x;tempx<(x+4);tempx++)
  243.             {
  244.                 SetPixelV(gdicIso,tempx,y,crColor);//set color on iso picture
  245.                 SetPixelV(gdicIsoMask,tempx,y,0);//set to black on mask
  246.             }
  247.         }
  248.     }
  249.  
  250.     //show the map
  251.     ShowMap(hdc);
  252.  
  253.     //release the hdc back to the main window
  254.     ReleaseDC(hWndMain,hdc);
  255.  
  256.     return(true);//return success
  257. }
  258.  
  259. //////////////////////////////////////////////////////////////////////////////
  260. //CLEANUP
  261. //////////////////////////////////////////////////////////////////////////////
  262. void Prog_Done()
  263. {
  264. }
  265.  
  266. //////////////////////////////////////////////////////////////////////////////
  267. //MAIN GAME LOOP
  268. //////////////////////////////////////////////////////////////////////////////
  269. void Prog_Loop()
  270. {
  271. }
  272.  
  273. void ShowMap(HDC hdc)
  274. {
  275.     //plot iso tiles using staggered calculations
  276.     for(int y=0;y<20;y++)
  277.     {
  278.         for(int x=0;x<20;x++)
  279.         {
  280.             //if iso
  281.             if(bShowIso)
  282.             {
  283.                 //put mask
  284.                 BitBlt(hdc,x*64+(y&1)*32-32,y*16-16,64,32,gdicIsoMask,0,0,SRCAND);
  285.                 //put image
  286.                 BitBlt(hdc,x*64+(y&1)*32-32,y*16-16,64,32,gdicIso,0,0,SRCINVERT);
  287.             }
  288.             else
  289.             {
  290.                 //square
  291.                 BitBlt(hdc,x*16,y*16,16,16,gdicSquare,0,0,SRCCOPY);
  292.             }
  293.         }
  294.     }
  295. }
  296.